package com.jzxy.PingFen.handel;

import com.alibaba.fastjson.JSON;
import com.jzxy.PingFen.common.Result;
import com.jzxy.PingFen.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName:AuthenticationEntryPointImpl
 * Package:com.jzxy.PingFen.handel
 * Description:
 *
 * @Author: dlh
 * @Creat:2025/1/16-21:25
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Result result = new Result(HttpStatus.UNAUTHORIZED.value(),"用户认证失败请查询登录",null);
        String json = JSON.toJSONString(result);
        // 处理异常
        WebUtils.renderString(httpServletResponse, json);
    }
}
